package com.cskaoyan.realm;

import com.cskaoyan.bean.User;
import com.cskaoyan.service.UserManagementService;
import com.cskaoyan.vo.InfoVO;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author LiaoLong
 * @date 2021-05-12 10:19
 */
@Component
public class WxRealm extends AuthorizingRealm {

    @Autowired
    private UserManagementService userManagementService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        MallToken token = (MallToken) authenticationToken;
        String username = token.getUsername();
        // username要唯一
        String credential = userManagementService.getPasswordByName(username);
        return new SimpleAuthenticationInfo(username, credential, this.getName());
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        // // TODO: 配置用户权限, 查询用户权限
        // String username = (String) principalCollection.getPrimaryPrincipal();
        // //查询username对应的权限

        // InfoVO infoVO = userManagementService.queryByUsername(username);
        // List<String> roles = infoVO.getRoles();
        // List<String> perms = infoVO.getPerms();
        // SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        // // 给该用户添加权限
        // authorizationInfo.addRoles(roles);
        // authorizationInfo.addStringPermissions(perms);
        // return authorizationInfo;
        return null;
    }

    // private AuthenticationInfo dealInfoByType(MallToken token){
    //     String type = token.getType();
    //     String username = token.getUsername();
    //     if ("admin".equals(type)){
    //         String credential = userManagementService.getPasswordByName(username);
    //         return new SimpleAuthenticationInfo(username, credential, this.getName());
    //     }
    //
    //     if ("wx".equals(type)){
    //         String credential = userManagementService.getPasswordByName(username);
    //         return new SimpleAuthenticationInfo(username, credential, this.getName());
    //     }
    //     return null;
    // }
}
